/**
 * 08/dic/2011 - fabio.melillo@gmail.com
 */
package demo.unisannio;

import org.unisannio.remote.GenericRemote;
import org.unisannio.remote.RemoteContentProvider;
import org.unisannio.util.MovieMetaData;

import android.app.Activity;
import android.app.ProgressDialog;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.widget.Toast;

/**
 * @author fabio
 * 
 */
public class GetMoviesTask extends AsyncTask<GenericRemote, Integer, Cursor> {

	private Activity activity;
	private ProgressDialog dialog;
	private String title;
	public GetMoviesTask(Activity activity,String title) {
		this.activity = activity;
		this.title=(title=="") ? "%" : title;
	}

	@Override
	protected void onPreExecute() {
		dialog = ProgressDialog.show(activity, "",
				"Querying. Please wait...", true);
	}

	@Override
	protected Cursor doInBackground(GenericRemote... remote) {
		Uri uri = MovieMetaData.CONTENT_URI;
		String[] valori = new String[1];
		valori[0] = title;
		String where=MovieMetaData.MOVIE_TITLE_KEY+ " LIKE ? ";
		Cursor cursor = ((RemoteContentProvider) remote[0]).query(uri, null,
				where, valori, null);
		return cursor;
	}

	@Override
	protected void onPostExecute(Cursor cursor) {
		dialog.dismiss();
		if (cursor == null) {
			Toast toast = Toast
					.makeText(
							activity,
							"Il Peer selezionato potrebbe essersi allontanato",
							Toast.LENGTH_LONG);
			toast.show();
		} else {
			cursor.moveToFirst();
			int i = 0;
			while (!cursor.isAfterLast()) {
				i++;
				cursor.moveToNext();
				
			}

			Toast toast = Toast.makeText(activity, "Letti: " + i,
					Toast.LENGTH_LONG);
			toast.show();

		}
	}

}
